package com.zjlp.seproject.controller;


import com.zjlp.seproject.pojo.*;
import com.zjlp.seproject.service.SubjectService;
import com.zjlp.seproject.service.UserService;
import com.zjlp.seproject.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private UserService userService;

    /**
     *
     * @param authorization  请求头token
     * @param response  后端相应
     * @return  返回SubjectDetails对象
     */
    @RequestMapping("/getNewSubject")
    public List<SubjectDetails> getNewSubject(@RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return null;
        }
        String id = JwtUtil.getUserId(authorization);
        User isAdmin = userService.admin(Integer.parseInt(id));
        if (isAdmin == null) {
            response.setStatus(999);
            return null;
        }
        return subjectService.getNewSubject();
    }

    /**
     *
     * @param id  对应项目id
     * @param authorization  请求头token
     * @param response  后端相应
     */
    @RequestMapping("/allowSubject")
    public void allowSubject(@RequestBody ID id,
                             @RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        System.out.println("allowSubject");
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return;
        }
        String idStr = JwtUtil.getUserId(authorization);
        User isAdmin = userService.admin(Integer.parseInt(idStr));
        if (isAdmin == null) {
            response.setStatus(999);
            return;
        }
        subjectService.allowSubject(id.getId());
    }

    /**
     *
     * @param id   需要拒绝的项目id
     * @param authorization  请求头token
     * @param response  后端相应
     * @return  返回SubjectDetails对象
     */
    @RequestMapping("/adminSubject")
    public SubjectDetails adminSubject(@RequestBody ID id,
                             @RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        System.out.println("adminSubject");
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return null;
        }
        String idStr = JwtUtil.getUserId(authorization);
        if (idStr == null) {
            response.setStatus(999);
            return null;
        }
        User isAdmin = userService.admin(Integer.parseInt(idStr));
        if (isAdmin == null) {
            response.setStatus(999);
            return null;
        }
        SubjectDetails sd = subjectService.adminSubject(id.getId());
        String markdown = "";
        String thisLine;
        String path = sd.getDescription();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            while((thisLine = br.readLine()) != null) {
                markdown += thisLine + "\n";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(markdown);
        sd.setDescription(markdown);
        return sd;
    }

    @RequestMapping("/refuseSubject")
    public void refuseSubject(@RequestBody ID id,
                              @RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        System.out.println("refuseSubject");
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return;
        }
        String idStr = JwtUtil.getUserId(authorization);
        User isAdmin = userService.admin(Integer.parseInt(idStr));
        if (isAdmin == null) {
            response.setStatus(999);
            return;
        }
        subjectService.refuseSubject(id.getId());
    }

    /**
     *
     * @param id   需要删除的项目id
     */
    @RequestMapping("/deleteSubject")
    public void deleteSubject(@RequestBody ID id) {
        System.out.println("deleteSubject");
        subjectService.deleteSubject(id.getId());
    }


}
